package org.leo.study.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static java.util.Calendar.DAY_OF_YEAR;

@Slf4j
public class NewDateUtil {
    public final static SimpleDateFormat SHORT_SDF = new SimpleDateFormat("yyyy-MM-dd");
    public final static SimpleDateFormat lONG_HOURS_DF = new SimpleDateFormat("yyyy-MM-dd HH");
    public final static SimpleDateFormat LONG_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    public final static SimpleDateFormat HOUR_SDF = new SimpleDateFormat("HH");
    public final static SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 时间格式 00:00:00.000
     */
    private static final String TIME_START_SSS = " 00:00:00.000";

    /**
     * 时间格式 23:59:59:999
     */
    private static final String TIME_END_SSS = " 23:59:59:999";

    /**
     * 时间格式 00:00:00
     */
    private static final String TIME_START = " 00:00:00";

    /**
     * 时间格式 23:59:59
     */
    private static final String TIME_END = " 23:59:59";


    /**
     * 时间格式 HH
     */
    public static final String FM_HH = "HH";

    /**
     * 时间格式 HH
     */
    public static final String FM_YYYY_MM_DD = "yyyy-MM-dd";

    /**
     * 时间格式 yyyy-MM-dd HH:mm:ss.SSS
     */
    public static final String FM_YYYY_MM_DD_HHMMSSSSS = "yyyy-MM-dd HH:mm:ss.SSS";

    /**
     * 时间格式 yyyy-MM-dd HH:mm:ss
     */
    public static final String FM_YYYY_MM_DD_HHMMSS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 功能描述:根据要求的格式，格式化(默认: yyyy-MM-dd HH:mm:ss)时间，返回String
     *
     * @param time 时间
     * @return java.lang.String(时间字符串)
     * @author Leo
     * @date 2019/10/19 15:51
     */
    public static String toStr(Date time) {
        return toStr(FM_YYYY_MM_DD_HHMMSSSSS, time);
    }

    /**
     * 功能描述:根据要求的格式，格式化时间，返回String
     *
     * @param format 时间格式 (默认: yyyy-MM-dd HH:mm:ss)
     * @param time   时间
     * @return java.lang.String(时间字符串)
     * @author Gidenon
     * @date 2019/7/18 15:51
     */
    public static String toStr(String format, Date time) {
        SimpleDateFormat df = null;
        if (null == format) {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        } else {
            df = new SimpleDateFormat(format);
        }
        try {
            return df.format(time);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            df = null;
        }
    }

    /**
     * 功能描述:根据时间格式转换时间字符串
     *
     * @param format 时间格式
     * @param time   时间字符串
     * @return (时间类型)
     * @author Leo
     * @date 2019/7/18 15:51
     */
    public static Date toDate(String format, String time) {
        if (null == format || "".equals(format.trim())) {
            throw new RuntimeException("param format is not null");
        }
        if (null == time || "".equals(time.trim())) {
            throw new RuntimeException("param time is not null");
        }
        SimpleDateFormat df = null;
        try {
            df = new SimpleDateFormat(format);
            return df.parse(time);
        } catch (ParseException e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            df = null;
        }
    }

    /*
     *
     * 功能描述:字符串转时间
     * @author Gidenon
     * @date 2019/7/18 15:51
     * @param [source]
     * @return java.util.Date(yyyy-MM-dd hh:mm:ss)
     */
    public static Date toDate(String source) {
        String formatString = FM_YYYY_MM_DD_HHMMSS;
        if (source == null || "".equals(source.trim())) {
            return null;
        }
        source = source.trim();
        if (source.matches("^\\d{4}$")) {
            formatString = "yyyy";
        } else if (source.matches("^\\d{4}-\\d{1,2}$")) {
            formatString = "yyyy-MM";
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
            formatString = "yyyy-MM-dd";
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}$")) {
            formatString = "yyyy-MM-dd hh";
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
            formatString = "yyyy-MM-dd hh:mm";
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
            formatString = "yyyy-MM-dd hh:mm:ss";
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}\\.\\d{1,3}$")) {
            formatString = "yyyy-MM-dd HH:mm:ss.SSS";
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(formatString);
            Date date = sdf.parse(source);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
     *
     * 功能描述:获得本小时的开始时间
     * @author Gidenon
     * @date 2019/7/18 15:51
     * @param [date]
     * @return java.util.Date
     */
    public static Date getHourStartTime(Date date) {
        Date dt = null;
        try {
            dt = lONG_HOURS_DF.parse(lONG_HOURS_DF.format(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /*
     *
     * 功能描述:获得本小时的结束时间
     * @author Gidenon
     * @date 2019/7/18 15:51
     * @param [date]
     * @return java.util.Date
     */
    public static Date getHourEndTime(Date date) {
        Date dt = null;
        try {
            dt = LONG_SDF.parse(lONG_HOURS_DF.format(date) + ":59:59.999");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * 功能描述: 获得本小时的开始时间
     *
     * @param date 时间
     * @return java.util.Date
     * @author Gidenon
     * @date 2019/7/18 15:50
     */
    public static Date getTwoHourStartTime(Date date) {
        Date dt = null;
        try {
            int hour = Integer.parseInt(toStr(FM_HH, date));
            hour = hour / 2 * 2;
            String hourStr;
            if (hour < 10) {
                hourStr = "0" + Integer.toString(hour);
            } else {
                hourStr = Integer.toString(hour);
            }
            dt = toDate(toStr(FM_YYYY_MM_DD, date) + " " + hourStr + ":00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /*
     *
     * 功能描述:获得两小时的结束时间
     * @author Gidenon
     * @date 2019/7/18 15:50
     * @param [date]
     * @return java.util.Date
     */
    public static Date getTwoHourEndTime(Date date) {
        Date dt = null;
        try {
            Integer hour = Integer.parseInt(HOUR_SDF.format(date));
            hour = hour / 2 * 2 + 1;
            String hourStr;
            if (hour < 10) {
                hourStr = "0" + hour.toString();
            } else {
                hourStr = hour.toString();
            }
            dt = LONG_SDF.parse(SHORT_SDF.format(date) + " " + hourStr + ":59:59.999");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /*
     *
     * 功能描述: 获得本天的开始时间
     * @author Gidenon
     * @date 2019/7/18 15:50
     * @param [date]
     * @return java.util.Date
     */
    public static Date getDayStartTime(Date date) {
        Date dt = null;
        try {
            dt = SHORT_SDF.parse(SHORT_SDF.format(date));
            if (date.compareTo(dt) < 0) {
                dt = date;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /*
     *
     * 功能描述:获得本天的结束时间
     * @author Gidenon
     * @date 2019/7/18 15:50
     * @param [date]
     * @return java.util.Date
     */
    public static Date getDayEndTime(Date date) {
        Date dt = null;
        try {
            dt = LONG_SDF.parse(SHORT_SDF.format(date) + " 23:59:59.999");
            if (date.compareTo(dt) > 0) {
                dt = date;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /*
     *
     * 功能描述:获取当前时间是星期几
     * @author Gidenon
     * @date 2019/7/18 15:50
     * @param [date]
     * @return int
     */
    public static int getWeekDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int week_of_year = c.get(Calendar.DAY_OF_WEEK);
        return week_of_year - 1;
    }

    /*
     *
     * 功能描述:获得当前周的第一天，周一
     * @author Gidenon
     * @date 2019/7/18 15:50
     * @param [date]
     * @return java.util.Date
     */
    public static Date getWeekStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
            c.add(Calendar.DATE, -weekday);
            c.setTime(toDate(FM_YYYY_MM_DD_HHMMSSSSS, toStr(FM_YYYY_MM_DD, c.getTime()) + " 00:00:00.000"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }

    /*
     *
     * 功能描述:获得当前周的最后一天，周日
     * @author Gidenon
     * @date 2019/7/18 15:49
     * @param [date]
     * @return java.util.Date
     */
    public static Date getWeekEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK);
            c.add(Calendar.DATE, 8 - weekday);
            c.setTime(LONG_SDF.parse(SHORT_SDF.format(c.getTime()) + " 23:59:59.999"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }

    /*
     *
     * 功能描述:获得当前月的开始时间
     * @author Gidenon
     * @date 2019/7/18 15:49
     * @param [date]
     * @return java.util.Date
     */
    public static Date getMonthStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Date dt = null;
        try {
            c.set(Calendar.DATE, 1);
            dt = SHORT_SDF.parse(SHORT_SDF.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /*
     *
     * 功能描述:获取当前月的结束时间
     * @author Gidenon
     * @date 2019/7/18 15:49
     * @param [date]
     * @return java.util.Date
     */
    public static Date getMonthEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Date dt = null;
        try {
            c.set(Calendar.DATE, 1);
            c.add(Calendar.MONTH, 1);
            c.add(Calendar.DATE, -1);
            dt = LONG_SDF.parse(SHORT_SDF.format(c.getTime()) + " 23:59:59.999");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /*
     *
     * 功能描述:获取当前季度的开始时间
     * @author Gidenon
     * @date 2019/7/18 15:49
     * @param [date]
     * @return java.util.Date
     */
    public static Date getQuarterStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date dt = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 6);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            dt = LONG_SDF.parse(SHORT_SDF.format(c.getTime()) + " 00:00:00.000");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /*
     *
     * 功能描述:获取当前季度的结束时间
     * @author Gidenon
     * @date 2019/7/18 15:48
     * @param [date]
     * @return java.util.Date
     */
    public static Date getQuarterEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date dt = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            dt = LONG_SDF.parse(SHORT_SDF.format(c.getTime()) + " 23:59:59.999");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /*
     *
     * 功能描述:获取当前年前/后半年的开始时间
     * @author Gidenon
     * @date 2019/7/18 15:48
     * @param [date]
     * @return java.util.Date
     */
    public static Date getHalfYearStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date dt = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 0);
            } else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 6);
            }
            c.set(Calendar.DATE, 1);
            dt = LONG_SDF.parse(SHORT_SDF.format(c.getTime()) + " 00:00:00.000");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;

    }

    /*
     *
     * 功能描述:获取当前年的前/后半年的结束时间
     * @author Gidenon
     * @date 2019/7/18 15:48
     * @param [date]
     * @return java.util.Date
     */
    public static Date getHalfYearEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date dt = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            dt = LONG_SDF.parse(SHORT_SDF.format(c.getTime()) + " 23:59:59.999");
            if (date.compareTo(dt) > 0) {
                dt = date;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /*
     *
     * 功能描述:获取当前年的开始时间
     * @author Gidenon
     * @date 2019/7/18 15:48
     * @param [date]
     * @return java.util.Date
     */
    public static Date getYearStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Date dt = null;
        try {
            c.set(Calendar.MONTH, 0);
            c.set(Calendar.DATE, 1);
            dt = SHORT_SDF.parse(SHORT_SDF.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /*
     *
     * 功能描述:获取当前年的结束时间
     * @author Gidenon
     * @date 2019/7/18 15:47
     * @param [date]
     * @return java.util.Date
     */
    public static Date getYearEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Date dt = null;
        try {
            c.set(Calendar.MONTH, 11);
            c.set(Calendar.DATE, 31);
            dt = LONG_SDF.parse(SHORT_SDF.format(c.getTime()) + " 23:59:59.999");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /*
     *
     * 功能描述:当前时间属于本年第几天
     * @author Gidenon
     * @date 2019/7/18 15:47
     * @param [date]
     * @return int
     */
    public static int getYearDayIndex(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        return calendar.get(DAY_OF_YEAR);
    }

    /*
     *
     * 功能描述:当前时间属于本年第几月
     * @author Gidenon
     * @date 2019/7/18 15:47
     * @param [date]
     * @return int
     */
    public static int getYearWeekIndex(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    /*
     *
     * 功能描述:当前时间属于本年第几月
     * @author Gidenon
     * @date 2019/7/18 15:47
     * @param [date]
     * @return int
     */
    public static int getYearMonthIndex(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /*
     *
     * 功能描述:当前时间属于本年第几个季度
     * @author Gidenon
     * @date 2019/7/18 15:46
     * @param [date]
     * @return int
     */
    public static int getYeartQuarterIndex(Date date) {
        int month = getYearMonthIndex(date);
        if (month <= 3)
            return 1;
        else if (month <= 6)
            return 2;
        else if (month <= 9)
            return 3;
        else
            return 4;
    }

    /**
     * 计算两个日期之间相差几天
     *
     * @param fromTime 开始
     * @param toTime   结束
     * @return 相差天数
     * @author Leo
     * @date 2019/10/19 17:16
     */
    public static int getDifferentDays(Date fromTime, Date toTime) {
        int days = (int) ((toTime.getTime() - fromTime.getTime()) / (1000 * 3600 * 24));
        return days;
    }

    /**
     * 功能描述:获取时间段内的两小时开始时间和结束时间
     *
     * @param fromTime 开始时间
     * @param toTime   结束时间周
     * @return java.util.List<java.util.Date [ ]>{[startTime,endTime],[startTime,endTime]}
     * @author Gidenon
     * @date 2019/7/18 15:43
     */
    public static List<Date[]> yearTowHoursList(Date fromTime, Date toTime) {
        List<Date[]> result = new ArrayList<Date[]>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getTwoHourStartTime(fromTime);
        calendar.setTime(starttm);
        while (calendar.getTime().before(toTime)) {
            Date st = getTwoHourStartTime(calendar.getTime());
            Date et = getTwoHourEndTime(calendar.getTime());
            if (st.compareTo(fromTime) < 0) {
                st = fromTime;
            }
            if (et.compareTo(toTime) > 0) {
                et = toTime;
            }
            result.add(new Date[]{st, et});
            calendar.add(Calendar.HOUR, 2);
        }
        return result;

    }

    /**
     * 功能描述：按照指定分割数将时间段分割为多段
     *
     * @param fromTime    开始时间
     * @param toTime      结束时间
     * @param cutApartNum 分割数
     * @return java.util.List<java.util.Date [ ]>{[startTime,endTime],[startTime,endTime]}
     * @author Leo
     * @date 2019/10/19 16:39:53
     */
    public static List<Date[]> dayListByCutApart(Date fromTime, Date toTime, int cutApartNum) {
        if (cutApartNum < 0) {
            throw new RuntimeException("Param 'cutApartNum' cannot be less than 0");
        }
        if (fromTime == null) {
            throw new RuntimeException("Param 'fromTime' is not null");
        }
        if (toTime == null) {
            throw new RuntimeException("Param 'toTime' is not null");
        }

        List<Date[]> result = new ArrayList<Date[]>();
        int days = getDifferentDays(fromTime, toTime) / cutApartNum;
        Calendar calendar = Calendar.getInstance();
        Date starttm = getDayStartTime(fromTime);
        calendar.setTime(starttm);
        int i = 1;
        Date st = null;
        Date et = null;
        while (i <= cutApartNum) {
            st = calendar.getTime();
            calendar.add(Calendar.DAY_OF_YEAR, days);
            et = calendar.getTime();
            if (i == 1) {
                st = fromTime;
            }
            if (i == cutApartNum) {
                et = toTime;
            }
            result.add(new Date[]{st, et});
            i++;
            calendar.add(DAY_OF_YEAR, days < 0 ? 0 - 1 : 1);
        }
        return result;
    }

    /**
     * 功能描述：按照指定天数将时间段分割为多段
     *
     * @param fromTime
     * @param toTime
     * @param dayNum
     * @return
     */
    public static List<Date[]> dayListByDaySplit(Date fromTime, Date toTime, int dayNum) {
        List<Date[]> result = new ArrayList<Date[]>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getDayStartTime(fromTime);
        calendar.setTime(starttm);
        Date st = null;
        Date et = null;
        while (calendar.getTime().before(toTime)) {
            st = calendar.getTime();
            calendar.add(DAY_OF_YEAR, dayNum);
            et = calendar.getTime();
            if (st.compareTo(fromTime) < 0) {
                st = fromTime;
            }
            if (et.compareTo(toTime) > 0) {
                et = toTime;
            }
            result.add(new Date[]{st, et});
            calendar.add(DAY_OF_YEAR, 1);
        }

        return result;
    }

    /*
     *
     * 功能描述:获取时间段内的天开始时间和结束时间
     * @author Gidenon
     * @date 2019/7/18 15:43
     * @param [fromTime, toTime]
     * @return java.util.List<java.util.Date[]>{[startTime,endTime],[startTime,endTime]}
     */
    public static List<Date[]> yearDayList(Date fromTime, Date toTime) {
        List<Date[]> result = new ArrayList<Date[]>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getDayStartTime(fromTime);
        calendar.setTime(starttm);

        while (calendar.getTime().before(toTime)) {
            Date st = getDayStartTime(calendar.getTime());
            Date et = getDayEndTime(calendar.getTime());
            if (st.compareTo(fromTime) < 0) {
                st = fromTime;
            }
            if (et.compareTo(toTime) > 0) {
                et = toTime;
            }
            result.add(new Date[]{st, et});
            calendar.add(DAY_OF_YEAR, 1);
        }
        return result;

    }

    /**
     * 功能描述:获取时间段内的星期开始时间和结束时间
     *
     * @param fromTime 开始时间
     * @param toTime   结束时间
     * @return java.util.List<java.util.Date [ ]>{[startTime,endTime],[startTime,endTime]}
     * @author Gidenon
     * @date 2019/7/18 15:43
     */
    public static List<Date[]> yearWeekList(Date fromTime, Date toTime) {
        List<Date[]> result = new ArrayList<Date[]>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getWeekStartTime(fromTime);
        calendar.setTime(starttm);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        while (calendar.getTime().before(toTime)) {
            Date st = getWeekStartTime(calendar.getTime());
            Date et = getWeekEndTime(calendar.getTime());
            if (st.compareTo(fromTime) < 0) {
                st = fromTime;
            }
            if (et.compareTo(toTime) > 0) {
                et = toTime;
            }
            result.add(new Date[]{st, et});
            calendar.add(Calendar.WEEK_OF_YEAR, 1);
        }
        return result;

    }

    /*
     *
     * 功能描述:获取时间段内的月开始时间和结束时间
     * @author Gidenon
     * @date 2019/7/18 15:43
     * @param [fromTime, toTime]
     * @return java.util.List<java.util.Date[]>{[startTime,endTime],[startTime,endTime]}
     */
    public static List<Date[]> yearMonthList(Date fromTime, Date toTime) {
        List<Date[]> result = new ArrayList<Date[]>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getMonthStartTime(fromTime);
        calendar.setTime(starttm);
        while (calendar.getTime().before(toTime)) {
            Date tm = calendar.getTime();
            Date st = getMonthStartTime(tm);
            Date et = getMonthEndTime(tm);
            if (st.compareTo(fromTime) < 0) {
                st = fromTime;
            }
            if (et.compareTo(toTime) > 0) {
                et = toTime;
            }
            result.add(new Date[]{st, et});
            calendar.add(Calendar.MONTH, 1);
        }
        return result;
    }

    /*
     *
     * 功能描述:获取时间段内的季度开始时间和结束时间
     * @author Gidenon
     * @date 2019/7/18 15:43
     * @param [fromTime, toTime]
     * @return java.util.List<java.util.Date[]>{[startTime,endTime],[startTime,endTime]}
     */
    public static List<Date[]> yearQuarterList(Date fromTime, Date toTime) {
        List<Date[]> result = new ArrayList<Date[]>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getQuarterStartTime(fromTime);
        calendar.setTime(starttm);
        while (calendar.getTime().before(toTime)) {
            Date st = getQuarterStartTime(calendar.getTime());
            Date et = getQuarterEndTime(calendar.getTime());
            if (st.compareTo(fromTime) < 0) {
                st = fromTime;
            }
            if (et.compareTo(toTime) > 0) {
                et = toTime;
            }
            result.add(new Date[]{st, et});
            calendar.add(Calendar.MONTH, 3);
        }
        return result;
    }

    /*
     *
     * 功能描述:获取时间段内的半年开始时间和结束时间
     * @author Gidenon
     * @date 2019/7/18 15:43
     * @param [fromTime, toTime]
     * @return java.util.List<java.util.Date[]>{[startTime,endTime],[startTime,endTime]}
     */
    public static List<Date[]> yearHalfAYearList(Date fromTime, Date toTime) {
        List<Date[]> result = new ArrayList<Date[]>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getYearStartTime(fromTime);
        calendar.setTime(starttm);
        while (calendar.getTime().before(toTime)) {
            Date st = getHalfYearStartTime(calendar.getTime());
            Date et = getHalfYearEndTime(calendar.getTime());
            if (st.compareTo(fromTime) < 0) {
                st = fromTime;
            }
            if (et.compareTo(toTime) > 0) {
                et = toTime;
            }
            result.add(new Date[]{st, et});
            calendar.add(Calendar.MONTH, 6);
        }
        return result;
    }

    /*
     *
     * 功能描述:获取时间段内的年开始时间和结束时间
     * @author Gidenon
     * @date 2019/7/18 15:43
     * @param [fromTime, toTime]
     * @return java.util.List<java.util.Date[]>{[startTime,endTime],[startTime,endTime]}
     */
    public static List<Date[]> yearList(Date fromTime, Date toTime) {
        List<Date[]> result = new ArrayList<Date[]>();
        Calendar calendar = Calendar.getInstance();
        Date starttm = getYearStartTime(fromTime);
        calendar.setTime(starttm);
        while (calendar.getTime().before(toTime)) {
            Date st = getYearStartTime(calendar.getTime());
            Date et = getYearEndTime(calendar.getTime());
            if (st.compareTo(fromTime) < 0) {
                st = fromTime;
            }
            if (et.compareTo(toTime) > 0) {
                et = toTime;
            }
            result.add(new Date[]{st, et});
            calendar.add(Calendar.MONTH, 12);
        }
        return result;
    }
}
